#include "enterroomsrv.h"

EnterRoomReq::EnterRoomReq(const std::string &room,const std::string& password) : SimpleProtoSrv(PT_ENTER_ROOM)
{
    this->addString(room);
    this->addString(password);
}

ProtoSrv* EnterRoomReq::decode(ACE_InputCDR &cdr)
{
    SimpleProtoSrv* request = new EnterRoomReq();
    if(!SimpleProtoSrv::decode(cdr,request)){
        delete request;
        request = 0;
    }

    return request;
}

ProtoSrv* EnterRoomReq::execute(ACE_SOCK_Stream &peer)
{
    SimpleProtoSrv *resp = 0;

    std::string room;
    std::string passwd;

    if(this->getString(room,0) && this->getString(passwd,1)){
        resp = new SimpleProtoSrv(PT_RESP_ENTER_ROOM);
        int result = RoomManager::getInstance()->addClient2Room(peer,room,passwd);
        resp->addLong(result);

        if(result == E_SUCCESS){
            std::auto_ptr<SimpleProtoSrv> notify(new SimpleProtoSrv(PT_NOTIFY_ENTER_ROOM));
            notify->addString(room);
            TetrixServer::getInstance()->broadcast(notify);
        }
    }

    return resp;
}

